/*
 * @Author: wei_jt@ecidi.com
 * @Date: 2019-11-01 11:28:49
 * @LastEditors: fisher
 * @LastEditTime: 2024-01-15 16:06:42
 * @Description: 路由根配置
 */
import Vue from 'vue'
import Router from 'vue-router'
import { addAlias } from '@/utils/route'

Vue.use(Router)

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * title: 'title'                 the name show in sidebar and breadcrumb (recommend set)
 * icon: 'svg-name'               the icon show in the sidebar
 * redirect
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 *  type:                         route/iframe/link
 *  activeMenu: '/Example/list'   the navigation will highlight the path you set                         
  *  query                        
  *  params                       
  *  config: [
  * 'invisible',                  will not in the navigation
  * 'untag',                      will not in the tag bar
  * 'fullScreen',                 page will fill the whole screen; only used to the first level menu
  * 'merge',                      name will be merged in only child case
  * 'noCache',                    the page will no be cached(default is false) used in keep-alive
  * 'affix',                      the tag will affix in the tags-view
  * 'token']                      the page parameter will carry 'fawkes-token'
  }
 */

/**
 * @description: 自定义路由,可自行修改
 *               任何情况下都会加载
 */
export const customRoutes = [
  //公开表单
  {
    path: '/sub_app_wp/public_form',
    name: 'public_form',
    // component: () => import('@/modules/PublicForm/PublicFormPage.vue'),
    meta: { config: ['fullScreen'] },
    children: [
      {
        name: 'publicFormAdd',
        path: ':formKey',
        // component: () => import('@/modules/PublicForm/PublicForm.vue'),
        meta: { config: ['fullScreen'] }
      },
      {
        name: 'publicFormEdit',
        path: ':formKey/:bizId',
        // component: () => import('@/modules/PublicForm/PublicForm.vue'),
        meta: { config: ['fullScreen'] }
      },
      {
        name: 'publicFormError',
        path: '/sub_app_wp/public_form_error',
        // component: () => import('@/modules/PublicForm/Error/error.vue'),
        meta: { config: ['fullScreen'] }
      },
      {
        name: 'publicFormSuccess',
        path: '/sub_app_wp/public_form_success',
        // component: () => import('@/modules/PublicForm/Success/success.vue'),
        meta: { config: ['fullScreen'] }
      }
    ]
  }
]

/**
 * @description: 仅在 真实作为子应用 运行时加载  (开启子应用模式，并通过主应用访问该应用时才视作真实子应用)
 *               线上/本地路由状态下都会加载
 */
export const microappRoutes = []

/**
 * @description: 不建议修改
 *               仅在 非真实作为子应用 运行时加载   (开启子应用模式，并通过主应用访问该应用时才视作真实子应用)
 *               线上/本地路由状态下都会加载
 */
export const baseRoutes = [
  {
    path: '/map',
    name: 'map',
    component: () => import('@/modules/DataManage/map/index.vue'),
    meta: { config: ['fullScreen'] }
  },
  {
    path: '/',
    redirect: '/login',
    meta: { config: ['invisible', 'untag'] }
  },
  {
    path: '/distribute',
    meta: { config: ['fullScreen'] }
  },
  {
    path: '/default',
    name: 'defualt',
    title: 'HOME',
    component: () => import('@/modules/Home'),
    meta: { config: ['invisible', 'untag'] }
  },
  //双因子登录页面
  {
    path: '/relogin',
    name: 'relogin',
    component: () => import('@/modules/Login/Relogin'),
    meta: { config: ['fullScreen'] }
  },
  //统一认证登录
  {
    path: '/unified',
    name: 'unified',
    component: () =>
      import('@/modules/UnifiedAuthentication/UnifiedLogin/index'),
    meta: { config: ['fullScreen'] }
  },
  //统一认证-单点登录登录
  {
    path: '/testUnified',
    name: 'testUnified',
    component: () => import('@/modules/TestUnifiedAuthentication/index'),
    meta: { config: ['fullScreen'] }
  },
  //统一认证登录成功跳转
  {
    path: '/authenticationSuccess',
    name: 'authenticationSuccess',
    component: () =>
      import('@/modules/UnifiedAuthentication/authenticationSuccess/index'),
    meta: { config: ['fullScreen'] }
  },
  //回调跳转内部系统示例
  {
    path: '/internal',
    name: 'internal',
    component: () => import('@/modules/SystemDemo/Redirect/index'),
    meta: { config: ['fullScreen'] }
  },
  {
    path: '/activate',
    name: 'activate',
    component: () => import('@/modules/SystemInformation/License/Activate'),
    meta: { config: ['fullScreen'] }
  },
  {
    path: '/loginPage',
    name: 'loginPage',
    redirect: '/login',
    component: () => import('@/modules/Login'),
    meta: { config: ['fullScreen'] },
    children: [
      {
        path: '/login',
        name: 'login',
        component: () => import('@/modules/Login/Login'),
        meta: { config: ['fullScreen'] }
      },
      {
        path: '/forget',
        name: 'forget',
        component: () => import('@/modules/Login/Forget'),
        meta: { config: ['fullScreen'] }
      },
      {
        path: '/register',
        name: 'register',
        component: () => import('@/modules/Login/Register'),
        meta: { config: ['fullScreen'] }
      },
      {
        path: '/bind',
        name: 'bind',
        component: () => import('@/modules/Login/Bind'),
        meta: { config: ['fullScreen'] }
      }
    ]
  },
  //个人中心
  {
    path: '/personal',
    name: 'personal',
    title: 'PERSONAL_CENTER',
    component: () => import('@/modules/PersonalCenter/index.vue'),
    children: [
      {
        path: 'profile',
        name: 'privacyCenter',
        component: () =>
          import('@/modules/PersonalCenter/PrivacyCenter/index.vue'),
        title: 'PERSONAL_CENTER'
      },
      {
        path: 'message',
        name: 'MessageCenter',
        component: () =>
          import('@/modules/PersonalCenter/MessageCenter/index.vue'),
        title: 'MESSAGE_CENTER'
      }
    ]
  },
  {
    path: '/error',
    name: 'error',
    redirect: '/404',
    component: () => import('@/modules/Error/index.vue'),
    meta: { config: ['fullScreen'] },
    children: [
      {
        name: '404',
        path: '/404',
        component: () => import('@/modules/Error/Error.vue'),
        props: { type: 404 },
        meta: { config: ['fullScreen'] }
      },
      {
        name: '403',
        path: '/403',
        component: () => import('@/modules/Error/Error.vue'),
        props: { type: 403 },
        meta: { config: ['fullScreen'] }
      },
      {
        name: '500',
        path: '/500',
        component: () => import('@/modules/Error/Error.vue'),
        props: { type: 500 },
        meta: { config: ['fullScreen'] }
      }
    ]
  }
]

export function createRouter() {
  //作为子应用运行时不加载基础路由
  let newBaseRoutes = Vue.prototype.$InQianKun ? microappRoutes : baseRoutes

  //加载自定义路由
  newBaseRoutes = newBaseRoutes.concat(customRoutes)

  if (Vue.prototype.$subApp) {
    for (let i = 0; i < newBaseRoutes.length; i++) {
      addAlias(newBaseRoutes[i])
    }
  }
  return new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({
      y: 0
    }),
    routes: [...newBaseRoutes]
    // base:'app'
  })
}

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export function setRoutes(routes) {
  resetRouter()

  if (Vue.prototype.$subApp) {
    for (let i = 0; i < routes.length; i++) {
      addAlias(routes[i])
    }
  }

  router.addRoutes ? router.addRoutes(routes) : router.addRoute(routes)
}

// 重写Push方法
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  let prefix = Vue.prototype.$appBasePath

  if (typeof location === 'string' && !location.includes(prefix)) {
    location = prefix + location
  }

  if (
    typeof location === 'object' &&
    !location.name &&
    !location.path.includes(prefix)
  ) {
    if (!(location.meta?.keepOrigin || location.query?.keepOrigin)) {
      location.path = prefix + location.path
    }
  }

  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject)
  }

  return originalPush.call(this, location).catch((err) => err)
}

//重写Replace方法
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace(location, onResolve, onReject) {
  let prefix = Vue.prototype.$appBasePath

  if (typeof location === 'string' && !location.includes(prefix)) {
    location = prefix + location
  }

  if (
    typeof location === 'object' &&
    !location.name &&
    !location.path.includes(prefix)
  ) {
    if (!(location.meta?.keepOrigin || location.query?.keepOrigin)) {
      location.path = prefix + location.path
    }
  }

  if (onResolve || onReject) {
    return originalReplace.call(this, location, onResolve, onReject)
  }

  return originalReplace.call(this, location).catch((err) => err)
}

const router = createRouter()

export default router
